package com.hoopchina.util;

import java.sql.Array;

import com.hoopchina.MainActivity;
import com.hoopchina.news.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Dialog utility class
 * @author Si Wei
 *
 */
public class DialogUtils {
	private static MainActivity            mMainAct;
	
	/**
	 * Set context
	 * @param aMainAct
	 */
	public static void setContext( MainActivity aMainAct ) {
		mMainAct = aMainAct;
	}
	
	/**
	 * Alert dialog with Yes/No buttons
	 * @param aContext parent
	 * @param aMessage message string
	 * @param aOKHandler yes button handler
	 * @param aCancelHandler no button handler
	 * @return dialog
	 */
	public static AlertDialog getQueryDialog(
			Context aContext,
			String aMessage,
			OnClickListener aOKHandler,
			OnClickListener aCancelHandler ) {
		LayoutInflater factory = 
			( LayoutInflater )aContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View view = factory.inflate( R.layout.commondialog, null);
		TextView txtMessage = ( TextView )view.findViewById( R.id.txtDialogMessage );
		txtMessage.setText( aMessage );
		AlertDialog dialog = new AlertDialog.Builder( aContext )
        .setPositiveButton( R.string.dlg_yes, aOKHandler )
        .setNeutralButton( R.string.dlg_no, aCancelHandler ).create();
		dialog.setView( view, 0, 0, 0, 0 );
		return dialog;
	}
	
	/**
	 * 
	 * @param aContext
	 * @param aMessage
	 * @return
	 */
	public static AlertDialog getNotifyDialog(
			Context aContext,
			String aMessage ) {
		return getNotifyDialog( 
				aContext, 
				aMessage,
				new android.content.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                } );
	}
	
	/**
	 * Notify dialog without buttons
	 * @param aContext parent
	 * @param aMessage message string
	 * @return dialog
	 */
	public static AlertDialog getNotifyDialog(
			Context aContext,
			String aMessage,
			OnClickListener aOKHandler ) {
		LayoutInflater factory = 
			( LayoutInflater )aContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View view = factory.inflate( R.layout.commondialog, null);
		TextView txtMessage = ( TextView )view.findViewById( R.id.txtDialogMessage );
		txtMessage.setText( aMessage );
		AlertDialog dialog = new AlertDialog.Builder( aContext )
			.setPositiveButton( ResourceUtils.getString( R.string.dlg_yes ), aOKHandler )
			.create();
		dialog.setView( view, 0, 0, 0, 0 );
		return dialog;
	}

	
	/**
	 * Show waiting dialog
	 * @param aMessage
	 */
	public static void showWaitingDialog( String aMessage ) {
		mMainAct.showWaitingDialog( aMessage );
	}
	
	/**
	 * Close waiting dialog
	 */
	public static void dismissWaitingDialog() {
		mMainAct.dismissWaitingDialog();
	}
	
	/**
	 * Show toast
	 * @param aMessage
	 * @param isLong
	 */
	public static void showToast( String aMessage, boolean isLong ) {
		LayoutInflater factory = 
			( LayoutInflater )mMainAct.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View view = factory.inflate( R.layout.mytoast, null);
		( (TextView) view.findViewById( R.id.txtToast ) )
		        .setText( aMessage ); 
		Toast toast = new Toast( mMainAct.getApplicationContext() );
		toast.setGravity( Gravity.CENTER_VERTICAL, 0, 0 );
		toast.setDuration( isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT );
		toast.setView( view );
		toast.show();
	}
	
	public static void showExitDialog(final Activity currentAct)
	{
		LayoutInflater inflater = ( LayoutInflater )currentAct.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
//		View layout = inflater.inflate(R.layout.exitdialogarea,
//		     (ViewGroup) currentAct.findViewById(R.id.exitdialogarea));
	    Dialog dialog = new AlertDialog.Builder(currentAct)
	    .setTitle("确认退出虎扑新闻？")
	    .setIcon(0)
//	    .setView(layout)
	    .setPositiveButton("确定",
		    new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
				int whichButton) {
				currentAct.finish();
				
				Intent exitIntent = new Intent(Intent.ACTION_MAIN);
				exitIntent.addCategory(Intent.CATEGORY_HOME);
				exitIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				currentAct.startActivity(exitIntent);
				
//			    System.exit(0);
			}
		    })
	    .setNeutralButton("取消",
		    new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
				int whichButton) {
			}
		    }).create();
	    dialog.show();
	}

}
